package com.example.luckin.slice.provider;

import com.example.luckin.ResourceTable;
import com.example.luckin.slice.entity.Category;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

public class CategoryProvider extends BaseItemProvider {
    private Context context;



    private int selectItem = 0;

    public int getSelectItem() {
        return selectItem;
    }

    public void setSelectItem(int selectItem) {
        this.selectItem = selectItem;
    }

    public CategoryProvider(Context context, List<Category> list) {
        this.context = context;
        this.list = list;
    }

    private List<Category> list;
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        if(component == null){
            component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_fraction_menu_category,componentContainer,false);
            CategoryHolder categoryHolder = new CategoryHolder();
            categoryHolder.txtCategory = (Text)component.findComponentById(ResourceTable.Id_txt_category);
            categoryHolder.txtNick = (Text) component.findComponentById(ResourceTable.Id_txt_nick);
            component.setTag(categoryHolder);
        }
        CategoryHolder categoryHolder = (CategoryHolder) component.getTag();
        categoryHolder.txtCategory.setText(list.get(i).getName());
        if(list.get(i).getNick()==null || list.get(i).getNick().equals("")){
            categoryHolder.txtNick.setVisibility(1);
        }
        else{
            categoryHolder.txtNick.setVisibility(0);
            categoryHolder.txtNick.setText(list.get(i).getNick());
        }
        if(i == selectItem){
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(255,255,255));
            component.setBackground(element);
            categoryHolder.txtCategory.setTextColor(Color.BLUE);
        }
        else {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(new RgbColor(0xcc,0xcc,0xcc));
            component.setBackground(element);
            categoryHolder.txtCategory.setTextColor(Color.BLACK);
        }
        return component;
    }



    private class CategoryHolder{
        private Text txtNick;
        private Text txtCategory;
    }
}
